package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Organization;
import cn.wolfcode.crm.mapper.OrganizationMapper;
import cn.wolfcode.crm.query.impl.OrganizationQuery;
import cn.wolfcode.crm.service.IOrganizationService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class OrganizationServiceImpl implements IOrganizationService {
    @Autowired
    OrganizationMapper organizationMapper;

    public void delete(Long id) {
        organizationMapper.deleteByPrimaryKey(id);
    }

    public void saveOrUpdate(Organization record) {
        if(record.getId()!=null){
        organizationMapper.updateByPrimaryKey(record);
        }else{
            organizationMapper.insert(record);
        }
    }

    public Organization get(Long id) {
        return organizationMapper.selectByPrimaryKey(id);
    }

    public List<Organization> getAll() {
        return organizationMapper.selectAll();
    }

    public void changeState(Long id) {
        organizationMapper.changeState(id);
    }

    public PageResult query(OrganizationQuery qo) {
        int rows = organizationMapper.queryForCount(qo);
        if (rows == 0) {
            new PageResult(rows , Collections.EMPTY_LIST);
        }
        List<Employee> result = organizationMapper.queryForList(qo);
        return new PageResult(rows,result);
    }

    @Override
    public Organization getByName(String name) {
        return organizationMapper.getByName(name);
    }
}
